﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using HtmlAgilityPack;
using System.IO;
using CSharpVerbalExpressions;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Threading;
using TrendingReader.Classes;

namespace TrendingReader.Test
{
    public partial class FrmGetChapterItems : Telerik.WinControls.UI.RadForm
    {
        public FrmGetChapterItems()
        {
            InitializeComponent();
        }

        private async void BtnGet_Click(object sender, EventArgs e)
        {
            await GetItems();
        }

        async Task GetItems()
        {
            await Task.Run(() => {

                var web = new HtmlWeb();
                HtmlAgilityPack.HtmlDocument doc;
                doc = web.Load(TxtUrl.Text);

                var htmlText = doc.DocumentNode.InnerHtml;
                var strLinks = string.Empty;
                string[] linksList = { };

                var verbEx = new VerbalExpressions()
                    .Then("var slides_page_url_path = [")
                    .Anything()
                    .Then("];");

                strLinks = verbEx.Capture(htmlText, "2");

                if (string.IsNullOrEmpty(strLinks))
                {
                    verbEx = new VerbalExpressions()
                    .Then("var slides_page_path = [")
                    .Anything()
                    .Then("];");

                    strLinks = verbEx.Capture(htmlText, "2");
                }

                if (string.IsNullOrEmpty(strLinks))
                {
                    return;
                }

                if (strLinks.Contains(","))
                {
                    linksList = strLinks.Split(',');
                }
                else if (strLinks.Contains("|"))
                {
                    linksList = strLinks.Split('|');
                }

                if (linksList.Length > 0)
                {
                    foreach (var link in linksList)
                    {
                        CommonFunctions.AddMessageToListBox(LsbItems, link.Replace("\"", ""));
                    }
                }
            });            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var _entities = new TrendingReaderEntities();
            var chaptersList = _entities.Chapters.OrderBy(s => s.UpdateDate).ToList();

            foreach (var item in chaptersList)
            {
                LsbItems.Items.Add(item.OrderIndex + " " + item.Name);
            }
        }

        private async void button2_Click(object sender, EventArgs e)
        {
            await Task.Run(() =>
            {
                for (int i = 0; i < 10; i++)
                {
                    LsbItems.Invoke((MethodInvoker)(() => LsbItems.Items.Add("Start Thread" + i.ToString())));
                    Thread tid1 = new Thread(() => Thread1(i));
                    tid1.Start();
                    Thread.Sleep(1000);
                }
            });
        }

        public void Thread1(int i)
        {
            for (int j = 0; j < 10; j++)
            {
                LsbItems.Invoke((MethodInvoker)(() => LsbItems.Items.Add(string.Format("Thread{0} - action {1}", i, j))));
                Thread.Sleep(1000);
            }
        }
    }
}
